import {
    reqUserLogin, reqUserRegister, reqSaveUserInfo, reqGetUserInfo, reqUploadAvatar,
    reqSpace
} from "@/api"
import { removeItem } from "@/utils/cookieUtil";
// state：仓库存储数据的地方
const state = {
    userInfo: {},
    roleList: [],
    spaceInfo: {}
}
// mutations：修改state的唯一手段，因此第一个参数必须是state，第二个参数为将要修改的数据
const mutations = {
    SET_USERINFO(state, res) {
        state.userInfo = res
        state.roleList = res.role
    },
    SET_SPACE(state, res) {
        state.spaceInfo = res
    },
    LOGOUT(state, res) {
        removeItem("token")
        state.userInfo = {}
        state.roleList = []
    }

}
// action：书写业务逻辑（method方法）
const actions = {
    async userLogin({ commit }, data) {
        return await reqUserLogin(data)
    },
    async userRegister({ commit }, data) {
        return await reqUserRegister(data)
    },
    async SaveUserInfo({ commit }, data) {
        return await reqSaveUserInfo(data)
    },
    async getUserInfo({ commit }, data) {
        let res = await reqGetUserInfo(data)
        if (res.success) commit("SET_USERINFO", res.data)
        return res
    },
    async uploadAvatar({ commit }, data) {
        return await reqUploadAvatar(data)
    },
    async getSpace({ commit }) {
        let res = await reqSpace()
        if (res.success) commit("SET_SPACE", res.data)
        return res
    }


}
// getters：计算属性
const getters = {

}

export default {
    state,
    mutations,
    actions,
    getters
}